//
//  UIView.swift
//  APP
//
//  Created by LXF on 2018/1/10.
//  Copyright © 2018年 Rdxer. All rights reserved.
//

import UIKit

public enum DashLineType {
    case vertical
    case horizontal
}

public class DashLineView:UIView{
    
    public convenience init() {
        self.init(frame: .zero)
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    var dashLineStrokeColor:UIColor = UIColor.lightGray
    var dashLineWidth:CGFloat = 2
    var dashLineType:DashLineType = .vertical
    var dashLinePhase:CGFloat = 0
    var dashLineLengths:[CGFloat] = [3,1]
    
    lazy var dashLineEndPoint:CGPoint = {
        if dashLineType == DashLineType.horizontal{
            return CGPoint.init(x: self.frame.origin.x + self.frame.width, y: 0)
        }
        return CGPoint.init(x: 0, y: self.frame.origin.y + self.frame.height)
    }()
    
    
    
    public override func draw(_ rect: CGRect) {
        super.draw(rect)
        let context = UIGraphicsGetCurrentContext()
        context?.setStrokeColor(dashLineStrokeColor.cgColor)
        context?.setLineWidth(dashLineWidth)
        context?.move(to: .init(x: 0, y: 0))
        
        context?.addLine(to: dashLineEndPoint)
        
        context?.setLineDash(phase: dashLinePhase, lengths: dashLineLengths)
        context?.drawPath(using: .stroke)
    }
}
